<?php
/**
 * @version     1.0.0
 * @package     com_glycophile
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Ali SAHLI <sahli28@gmail.com> - https://plus.google.com/u/0/115470606844807318192/posts
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');
define("PX_COMPATHIMAGE", JURI::root()."components/com_glycophile/asset/images/");

/**
 * View to edit
 */
class GlycophileViewInventaire extends JView
{
	protected $state;
	protected $item;
	protected $form;

	/**
	 * Display the view
	 */
	public function display($tpl = null)
	{
		$this->state	= $this->get('State');
		$this->item		= $this->get('Item');
		$this->form		= $this->get('Form');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
            throw new Exception(implode("\n", $errors));
		}

		$this->addToolbar();
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 */
	protected function addToolbar()
	{
		JFactory::getApplication()->input->set('hidemainmenu', true);

		$user		= JFactory::getUser();
		$isNew		= ($this->item->id == 0);
        if (isset($this->item->checked_out)) {
		    $checkedOut	= !($this->item->checked_out == 0 || $this->item->checked_out == $user->get('id'));
        } else {
            $checkedOut = false;
        }
		$canDo		= GlycophileHelper::getActions();

		JToolBarHelper::title(JText::_('COM_GLYCOPHILE_FORM_LBL_INVENTAIRE'), 'pays.png');

		// If not checked out, can save the item.
		if (!$checkedOut && ($canDo->get('core.edit')||($canDo->get('core.create'))))
		{

			JToolBarHelper::apply('inventaire.apply', 'JTOOLBAR_APPLY');
			JToolBarHelper::save('inventaire.save', 'JTOOLBAR_SAVE');
		}
		if (!$checkedOut && ($canDo->get('core.create'))){
			JToolBarHelper::custom('inventaire.save2new', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false);
		}
		// If an existing item, can save to a copy.
		if (!$isNew && $canDo->get('core.create')) {
			JToolBarHelper::custom('inventaire.save2copy', 'save-copy.png', 'save-copy_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false);
		}
		if (empty($this->item->id)) {
			JToolBarHelper::cancel('inventaire.cancel', 'JTOOLBAR_CANCEL');
		}
		else {
			JToolBarHelper::cancel('inventaire.cancel', 'JTOOLBAR_CLOSE');
		}

	}

   function getImage($id_invetaire, $type='N'){

    $imageName = "";
    if($type == 'R'){
       $query = "SELECT concat(s.id_pays, '/', t.code,'/', s.id, '/R', i.id , '.jpg') URL
						  FROM `#__glycophile_pays` p, #__glycophile_theme t, #__glycophile_serie s,
			              #__glycophile_sousserie ss, #__glycophile_inventaire i
						        WHERE t.identpays= s.id_pays
			              AND p.identpays = s.id_pays AND t.id= s.id_theme
			              AND ss.id_serie=s.id and i.id_sous_serie = ss.id
			              AND i.TailleX > 0 AND i.TailleY > 0
                    AND s.verso = 'O'
			              AND i.id=$id_invetaire
			              ORDER by p.identpays, t.code, s.id
              ";
    } else {
       $query = "SELECT concat(s.id_pays, '/', t.code,'/', s.id, '/', i.id , '.jpg') URL
						  FROM `#__glycophile_pays` p, #__glycophile_theme t, #__glycophile_serie s,
			              #__glycophile_sousserie ss, #__glycophile_inventaire i
						        WHERE t.identpays= s.id_pays
			              AND p.identpays = s.id_pays AND t.id= s.id_theme
			              AND ss.id_serie=s.id and i.id_sous_serie = ss.id
			              AND i.TailleX > 0 AND i.TailleY > 0
			              AND i.id=$id_invetaire
			              ORDER by p.identpays, t.code, s.id
              ";
    }


        $db = JFactory::getDbo();
				$db->setQuery($query);
				$imageName = $db->loadResult();
        if($imageName != ""){
          $imageName = PX_COMPATHIMAGE.$imageName;
        }else {
          $imageName = PX_COMPATHIMAGE."noimage.jpg";
        }
        return $imageName;

  }

}
